//导入 espress
const express = require("express");
const cors = require("cors");
const mysql = require("mysql");

const app = express();
app.use(cors());
const db = mysql.createPool({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "123446",
  database: "react_management",
});

//创建路由对象
const router = express.Router();

//创建路由
router.post("/reg", (req, res) => {
  const { username, password } = req.body;
  console.log(username, password);
  db.query(
    `select * from user where uid = '${username}' and password = '${password}'`,
    (err, results) => {
      if (err) {
        res.status(500);
        results = {
          warn: "error",
          message: "获取数据库时发生错误",
        };
        res.send(JSON.stringify(results));
      } else {
        res.send({
          status: 200,
          data: results,
        });
      }
    }
  );
});

//暴露 Router
module.exports = router;
